﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using ZXing.QrCode;
using ZXing;
using ZXing.Common;
using ZXing.Rendering;
using System.Drawing.Imaging;
namespace Microhard.FMCG.VerFakeCodeGenerator
{
    public partial class TwoCodeForm : Form
    {
        BarcodeWriter writer = null;
        public TwoCodeForm()
        {
            InitializeComponent();
            EncodingOptions options = new QrCodeEncodingOptions
            {
                DisableECI = true,
                CharacterSet = "UTF-8",
                Width = 800,
                Height = 800
            };
            writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = options;
        }

        public string savePath = "";
        public int numImage = 0;
        Thread SecondThread = null;

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                savePath = this.folderBrowserDialog1.SelectedPath + "\\";
                textBox1.Text = savePath;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        //生成二维码
        private void button2_Click(object sender, EventArgs e)
        {
            if (numImage > 0)
            {
                SecondThread = new Thread(new ThreadStart(PrintCodes));
                SecondThread.Start();
            }
        }

        void PrintCodes()
        {
            string fullPath = savePath + "code.txt";
            string s = "";

            for (int i = 1; i <= numImage; i++)
            {
                string path = savePath + i.ToString() + ".png";
                Guid id = Guid.NewGuid();
                string encString = Microhard.Core.Security.EncDec.Encrypt(id.ToString(), ConfigurationManager.AppSettings["FakeCodeKey"]);
                //组合成url
                string finalStr = ConfigurationManager.AppSettings["BasicUrl"] + System.Web.HttpUtility.UrlEncode(encString);
                //生成二维码
                Bitmap bitmap = writer.Write(finalStr);
                bitmap.Save(path, ImageFormat.Png);
                s = s + i.ToString() + "_" + id.ToString() + ",";
            }

            StreamWriter streamWriter = File.CreateText(fullPath);
            streamWriter.Write(s);
            streamWriter.Close();
            MessageBox.Show("二维码生成完成！");
            SecondThread.Abort();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                numImage = int.Parse(this.textBox2.Text);
            }
            catch
            {
                MessageBox.Show("数量输入错误！");
            }
        }

        public void CloseTask()
        {
            if (SecondThread != null)
            {
                SecondThread.Abort();
            }
        }

        private void TwoCodeForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.CloseTask();
        }
    }
}
